﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormNhanVien.xaml
    /// </summary>
    public partial class FormNhanVien : Page
    {
        private UpdateDB updateDB;
        private SelectDB selectDB;
        private string URL;
        private bool m_bSelectData= false;

        public FormNhanVien()
        {
            InitializeComponent();
            
        }

        public FormNhanVien(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            dataLoaiNhanVien.ItemsSource = selectDB.SelectAllDSLoaiNV(1);
            dataNhanVien.ItemsSource = selectDB.SelectAllDSNhanVien(1);
        }

        private void btnThemLoaiNV_Click(object sender, RoutedEventArgs e)
        {
            WinThemLoaiNV m_wThemLoaiNV = new WinThemLoaiNV(URL);
            m_wThemLoaiNV.Show();
        }

        
        private void btnSuaLoaiNV_Click(object sender, RoutedEventArgs e)
        {
            updateDB.UpdateLoaiNhanVien(txtMaLoaiNV.Text, txtTenLoaiNV.Text);
            m_bSelectData = true;
            dataLoaiNhanVien.ItemsSource = selectDB.SelectAllDSLoaiNV(1);
            dataNhanVien.ItemsSource = selectDB.SelectAllDSNhanVien(1);
        }

        private void dataLoaiNhanVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataLoaiNhanVien.SelectedItem as LoaiNhanVien;
                txtMaLoaiNV.Text = item.MaLoaiNhanVien.ToString();
                txtTenLoaiNV.Text = item.TenLoaiNhanVien.ToString();
            }
            else
            {
                m_bSelectData = false;
               
            }
        }

        private void btnThemNV_Click(object sender, RoutedEventArgs e)
        {
            WinThemNhanVien m_wThemNhanVien = new WinThemNhanVien(URL);
            m_wThemNhanVien.Show();
        }

        private void dataNhanVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataNhanVien.SelectedItem as NhanVien;
                txtMaNV.Text = item.MaNhanVien.ToString();
                txtTenLoaiNVNV.Text = item.MaLoaiNhanVien.ToString();
                txtTenNV.Text = item.TenNhanVien.ToString();
            }
            else
            {
                m_bSelectData = false;
               
            }
        }

        private void btnSuaNV_Click(object sender, RoutedEventArgs e)
        {

            if (!m_bSelectData)
                m_bSelectData = true;
            updateDB.UpdateNhanVien_TenNV(txtMaNV.Text, txtTenNV.Text);
            m_bSelectData = false;
            
            dataNhanVien.ItemsSource = selectDB.SelectAllDSNhanVien(1);
        }

        //xoa nhan vien thi cap nhat lai tinh trang la 1
        private void btnXoaNV_Click(object sender, RoutedEventArgs e)
        {

            if (!m_bSelectData)
                m_bSelectData = true;
            updateDB.UpdateNhanVien(txtMaNV.Text, 0);
            m_bSelectData = false;
            
            dataNhanVien.ItemsSource = selectDB.SelectAllDSNhanVien(1);
            
        }

        private void btnRefeshLoaiNV_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            dataLoaiNhanVien.ItemsSource = selectDB.SelectAllDSLoaiNV(1);
            
            m_bSelectData = false;
        }

        private void btnRefeshNhanVien_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            dataNhanVien.ItemsSource = selectDB.SelectAllDSNhanVien(1);
            m_bSelectData = false;
        }

    }
}
